const jwt = require("jsonwebtoken")
const secret = require("../key/secret.js")

// token生成
function generateToken(uid, time) {
    let token = jwt.sign(
        {
            uid
        },
        secret, 
        {
            expiresIn: time
        }
    )
    return token
}

// token验证
function verifyToken(token) {
    let result = jwt.verify(token, secret, (err, decoded) => {
        let id = decoded.uid // 用户id
        let exp = decoded.exp // 过期时间
        let nowTime = Math.floor(Date.now() / 1000) // 当前时间
        let res
        if (nowTime <= exp) {
            //根据用户id查数据库验证token
            // if() {}
            return true
        } else {
            return false
        }
    })
    return result
}

module.exports = {
    generateToken,
    verifyToken
}
